package com.hh.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.constants.Constants;
import com.hh.common.exception.HhException;
import com.hh.common.model.merchant.MerchantAdmin;
import com.hh.common.model.user.User;
import com.hh.common.request.RetailShopRequest;
import com.hh.common.utils.SecurityUtil;
import com.hh.service.dao.UserDao;
import com.hh.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * RetailShopServiceImpl 接口实现 分销业务实现
 * +----------------------------------------------------------------------
 * | hh [ hh赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.hh.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed hh并不是自由软件，未经许可不能去掉hh相关版权
 * +----------------------------------------------------------------------
 * | Author: hh Team <admin@hh.com>
 * +----------------------------------------------------------------------
 */
@Service
public class RetailShopServiceImpl extends ServiceImpl<UserDao, User> implements RetailShopService {

    @Autowired
    private SystemConfigService systemConfigService;

    /**
     * 获取分销列表
     * @param keywords 搜索参数
     * @param dateLimit 时间参数
     * @param pageRequest 分页参数
     */
//    @Override
//    public CommonPage<SpreadUserResponse> getSpreadPeopleList(String keywords, String dateLimit, PageParamRequest pageRequest) {
//        Page<User> pageUserPage = PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());
//        // id,头像，昵称，姓名，电话，推广用户数，推广订单数，推广订单额，佣金总金额，已提现金额，提现次数，未提现金额，上级推广人
//        PageInfo<User> userPageInfo = userService.getAdminSpreadPeopleList(keywords, dateLimit, pageRequest);
//
//        if (CollUtil.isEmpty(userPageInfo.getList())) {
//            return CommonPage.restPage(new PageInfo<>());
//        }
//        List<User> userList = userPageInfo.getList();
//        List<SpreadUserResponse> responseList = CollUtil.newArrayList();
//        userList.forEach(user -> {
//            SpreadUserResponse userResponse = new SpreadUserResponse();
//            BeanUtils.copyProperties(user, userResponse);
//            // 上级推广员名称
//            userResponse.setSpreadNickname("无");
//            if (ObjectUtil.isNotNull(user.getSpreadUid()) && user.getSpreadUid() > 0) {
//                User spreadUser = userService.getById(user.getSpreadUid());
//                userResponse.setSpreadNickname(Optional.ofNullable(spreadUser.getNickname()).orElse("--"));
//            }
//
//            List<UserBrokerageRecord> recordList = userBrokerageRecordService.getSpreadListByUid(user.getUid());
//            if (CollUtil.isEmpty(recordList)) {
//                // 推广订单数
//                userResponse.setSpreadOrderNum(0);
//                // 推广订单额
//                userResponse.setSpreadOrderTotalPrice(BigDecimal.ZERO);
//                // 佣金总金额
//                userResponse.setTotalBrokeragePrice(BigDecimal.ZERO);
//                // 已提现金额
//                userResponse.setExtractCountPrice(BigDecimal.ZERO);
//                // 提现次数
//                userResponse.setExtractCountNum(0);
//                // 冻结中佣金
//                userResponse.setFreezeBrokeragePrice(BigDecimal.ZERO);
//            } else {
//                // 推广订单数
//                userResponse.setSpreadOrderNum(recordList.size());
//                // 佣金总金额
//                userResponse.setTotalBrokeragePrice(recordList.stream().map(UserBrokerageRecord::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add));
//                // 推广订单额
//                List<String> orderNoList = recordList.stream().map(UserBrokerageRecord::getLinkId).collect(Collectors.toList());
//                BigDecimal spreadOrderTotalPrice = storeOrderService.getSpreadOrderTotalPriceByOrderList(orderNoList);
//                userResponse.setSpreadOrderTotalPrice(spreadOrderTotalPrice);
//
//                UserExtractResponse extractResponse = userExtractService.getUserExtractByUserId(user.getUid());
//                // 已提现金额
//                userResponse.setExtractCountPrice(extractResponse.getExtractCountPrice());
//                // 提现次数
//                userResponse.setExtractCountNum(extractResponse.getExtractCountNum());
//                // 冻结中佣金
//                userResponse.setFreezeBrokeragePrice(userBrokerageRecordService.getFreezePrice(user.getUid()));
//            }
//            responseList.add(userResponse);
//        });
//        PageInfo<SpreadUserResponse> responsePageInfo = CommonPage.copyPageInfo(pageUserPage, responseList);
//        responsePageInfo.setTotal(userPageInfo.getTotal());
//        responsePageInfo.setPages(userPageInfo.getPages());
//        return CommonPage.restPage(responsePageInfo);
//    }

    /**
     * 获取分销配置信息
     * @return 返回配置信息
     */
    @Override
    public RetailShopRequest getManageInfo(boolean isMer) {
        String merId = Constants.PLATFORM_MER_ID;

        if(isMer){
            MerchantAdmin user = SecurityUtil.getMerLoginUserVo().getUser();
            merId = user.getMerId();
        }

//        List<String> keys = initKeys();
        RetailShopRequest response = new RetailShopRequest();
        response.setBrokerageFuncStatus(Integer.parseInt(systemConfigService.getValueByKey("brokerage_func_status",isMer,merId)));
//        response.setStoreBrokerageStatus(systemConfigService.getValueByKey(keys.get(1)));
        response.setStoreBrokerageRatio(Integer.parseInt(systemConfigService.getValueByKey("store_brokerage_ratio",isMer,merId)));
        response.setStoreBrokerageTwo(Integer.parseInt(systemConfigService.getValueByKey("store_brokerage_two",isMer,merId)));
        response.setUserExtractMinPrice(new BigDecimal(systemConfigService.getValueByKey("user_extract_min_price",isMer,merId)));
        response.setUserExtractBank(systemConfigService.getValueByKey("user_extract_bank",isMer,merId).replace("\\n","\n"));
        response.setExtractTime(Integer.parseInt(systemConfigService.getValueByKey("extract_time",isMer,merId)));
        response.setStoreBrokerageQuota(Integer.parseInt(systemConfigService.getValueByKey("store_brokerage_quota",isMer,merId)));
        response.setStoreBrokerageIsBubble(Integer.parseInt(systemConfigService.getValueByKey("store_brokerage_is_bubble",isMer,merId)));
        response.setBrokerageBindind(Integer.parseInt(systemConfigService.getValueByKey("brokerage_bindind",isMer,merId)));
        return response;
    }

    /**
     * 更新分销配置信息
     * @param retailShopRequest 待保存数据
     * @return 更新结果
     */
    @Override
    public boolean setManageInfo(RetailShopRequest retailShopRequest,boolean isMer) {
        // 返佣比例之和+起来不能超过100%
        int ration = retailShopRequest.getStoreBrokerageTwo() + retailShopRequest.getStoreBrokerageRatio();
        if (ration > 100 || ration < 0) {
            throw new HhException("返佣比例加起来不能超过100%");
        }

//        List<String> keys = initKeys();
        systemConfigService.updateOrSaveValueByName("brokerage_func_status", retailShopRequest.getBrokerageFuncStatus().toString(),isMer);
//        systemConfigService.updateOrSaveValueByName("store_brokerage_status", retailShopRequest.getStoreBrokerageStatus());
        systemConfigService.updateOrSaveValueByName("store_brokerage_ratio", retailShopRequest.getStoreBrokerageRatio().toString(),isMer);
        systemConfigService.updateOrSaveValueByName("store_brokerage_two", retailShopRequest.getStoreBrokerageTwo().toString(),isMer);
        systemConfigService.updateOrSaveValueByName("user_extract_min_price", retailShopRequest.getUserExtractMinPrice().toString(),isMer);
        systemConfigService.updateOrSaveValueByName("user_extract_bank", retailShopRequest.getUserExtractBank(),isMer);
        systemConfigService.updateOrSaveValueByName("extract_time", retailShopRequest.getExtractTime().toString(),isMer);
//        systemConfigService.updateOrSaveValueByName(keys.get(7), retailShopRequest.getStoreBrokeragePrice().toString());
        systemConfigService.updateOrSaveValueByName("brokerage_bindind", retailShopRequest.getBrokerageBindind().toString(),isMer);
        systemConfigService.updateOrSaveValueByName("store_brokerage_quota", retailShopRequest.getStoreBrokerageQuota().toString(),isMer);
        systemConfigService.updateOrSaveValueByName("store_brokerage_is_bubble", retailShopRequest.getStoreBrokerageIsBubble().toString(),isMer);
        return true;
    }

}
